//
//  HYChatInputRecordAudioTipView.m
//  LEVE
//
//  Created by 上官惠阳 on 2017/3/18.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "HYChatInputRecordAudioTipView.h"
#import "GJCFCoreTextFrame.h"
#import "HYSpectrumView.h"

@interface HYChatInputRecordAudioTipView ()

@property (nonatomic,strong)GJCFCoreTextAttributedStringStyle *stringStyle;

@property (nonatomic,strong)GJCFCoreTextFrame *textFrame;

@end
@implementation HYChatInputRecordAudioTipView

- (instancetype)init
{
    if (self = [super init]) {

        [self setupStyle];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

        [self setupStyle];
    }
    return self;
}

- (void)dealloc
{
    [GJCFNotificationCenter removeObserver:self];
}

- (void)setupStyle
{
    self.voiceCancelImage = GJCFQuickImage(@"聊天语音-icon-取消.png");
//    self.voiceMicImage = GJCFQuickImage(@"聊天语音-icon-话筒.png");
//    self.voiceSoundMeterImage = GJCFQuickImage(@"聊天语音-icon-音量.png");

    CGFloat width = self.gjcf_width;
    CGFloat height = self.gjcf_height;

    CGFloat speHeight = 100;
    HYSpectrumView *spectrumView = [[HYSpectrumView alloc] initWithFrame:CGRectMake(0,(height - speHeight)/2,width,speHeight)];
    spectrumView.itemColor = [UIColor whiteColor];
    spectrumView.numberOfItems = 14;
    spectrumView.text = @"00:00";
    WeakSelf(ws);
    __weak HYSpectrumView * weakSpectrum = spectrumView;
    spectrumView.itemLevelCallback = ^() {
        weakSpectrum.level = ws.soundMeter * 10;
    };

    self.specView = spectrumView;
    [self addSubview:spectrumView];


    self.minRecordTimeErrorTitle = @"说话时间太短";
    self.maxRecordTimeErrorTitle = @"说话时间超长";
    self.upToCancelRecordTitle = @"手指上滑,取消发送";
    self.releaseToCancelRecordTitle = @"手指松开,取消发送";

    self.backgroundColor = GJCFQuickRGBColorAlpha(0, 0, 0, 0.5);
    self.gjcf_height = 178;
    self.gjcf_width = 178;
    self.center = (CGPoint){GJCFSystemScreenWidth/2,GJCFSystemScreenHeight/2};
    self.layer.cornerRadius = 8.0;
    self.layer.masksToBounds = YES;

    self.leftMargin = self.gjcf_width / 6;

    self.stringStyle = [[GJCFCoreTextAttributedStringStyle alloc]init];
    self.stringStyle.foregroundColor = [UIColor whiteColor];
    self.stringStyle.strokeColor = [UIColor whiteColor];
    self.stringStyle.font = [UIFont boldSystemFontOfSize:14];

    /* 观察程序进入后台了 */
    [GJCFNotificationCenter addObserver:self selector:@selector(observeAppResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
    [GJCFNotificationCenter addObserver:self selector:@selector(observeAppResignActive:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

- (void)drawRect:(CGRect)rect
{

    /* 录音时间太短 */
    if (self.isTooShortRecordDuration || self.isTooLongRecordDuration) {

        NSString *errorText = @" ！";

        CGContextRef context = GJCFContextRefTextMatrixFromView(self);

        CGFloat errorWidth = 50;
        CGFloat tipHeight = 30;
        CGFloat tipWidth = 100;

        CGRect errorTextRect = CGRectMake((self.gjcf_width - errorWidth)/2,(self.gjcf_height - errorWidth )/2 - tipHeight, errorWidth, errorWidth);

        self.stringStyle.font = [UIFont boldSystemFontOfSize:35];

        [self drawTitle:errorText inRect:errorTextRect inContext:context];

        CGRect tipRect = CGRectMake((self.gjcf_width - tipWidth)/2, errorTextRect.origin.y+errorTextRect.size.height, tipWidth, tipHeight);

        self.stringStyle.font = [UIFont boldSystemFontOfSize:16];

        if (self.isTooShortRecordDuration) {

            self.specView.hidden = YES;

            [self drawTitle:self.minRecordTimeErrorTitle  inRect:tipRect inContext:context];
        }

        if (self.isTooLongRecordDuration){

            self.specView.hidden = YES;

            [self drawTitle:self.maxRecordTimeErrorTitle inRect:tipRect inContext:context];
        }

        return;
    }

    /* 录音状态 */
    CGRect micRect = CGRectZero;
    if (!self.willCancel) {

        self.specView.hidden = NO;

        if (self.specView) {

            if (self.soundMeter > 0) {
                NSLog(@"self.soundMeter = %f",self.soundMeter);
                self.specView.level = self.soundMeter * 10;
            }

        }

    }

    /* 准备取消录音 */
    CGRect cancleRect = CGRectZero;
    if (self.willCancel) {

        self.specView.hidden = YES;

        CGSize cancelSize = self.voiceCancelImage.size;

        CGFloat originX = (self.bounds.size.width - cancelSize.width)/2;

        CGFloat originY = (self.bounds.size.height - cancelSize.height)/2;

        cancleRect = (CGRect){originX,originY,cancelSize.width,cancelSize.height};

        [self.voiceCancelImage drawInRect:cancleRect];

    }

    /* 获取文本图形上下文 */
    CGContextRef context = GJCFContextRefTextMatrixFromView(self);

    if (self.willCancel) {

        CGRect textRect = CGRectMake(self.leftMargin, 5, self.bounds.size.width - 2*self.leftMargin, 30);

        [self drawTitle:self.releaseToCancelRecordTitle inRect:textRect inContext:context];

    }else{

        CGRect textRect = CGRectMake(self.leftMargin, 5, self.bounds.size.width - 2*self.leftMargin, 30);

        [self drawTitle:self.upToCancelRecordTitle inRect:textRect inContext:context];
    }
}

- (void)drawTitle:(NSString *)title inRect:(CGRect)rect inContext:(CGContextRef)context
{
    NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc]initWithString:title attributes:[self.stringStyle attributedDictionary]];
    GJCFCoreTextParagraphStyle *paragraphStyle = [[GJCFCoreTextParagraphStyle alloc]init];
    paragraphStyle.alignment = kCTTextAlignmentCenter;
    [attriString addAttributes:[paragraphStyle paragraphAttributedDictionary] range:GJCFStringRange(title)];

    self.textFrame = [[GJCFCoreTextFrame alloc]initWithAttributedString:attriString withDrawRect:rect isNeedSetupLine:NO];

    [self.textFrame drawInContext:context];

    self.textFrame = nil;
}

#pragma mark - 设置属性影响
- (void)setSoundMeter:(CGFloat)soundMeter
{
    if (_soundMeter == soundMeter) {
        return;
    }
    _soundMeter = soundMeter;
    [self setNeedsDisplay];
}

- (void)setWillCancel:(BOOL)willCancel
{
    if (_willCancel == willCancel) {
        return;
    }
    _willCancel = willCancel;
    [self setNeedsDisplay];
}

- (void)setIsTooLongRecordDuration:(BOOL)isTooLongRecordDuration
{
    if (_isTooLongRecordDuration == isTooLongRecordDuration) {
        return;
    }
    _isTooLongRecordDuration = isTooLongRecordDuration;
    [self setNeedsDisplay];
}

- (void)setIsTooShortRecordDuration:(BOOL)isTooShortRecordDuration
{
    if (_isTooShortRecordDuration == isTooShortRecordDuration) {
        return;
    }
    _isTooShortRecordDuration = isTooShortRecordDuration;
    [self setNeedsDisplay];
}

#pragma mark - 观察程序进入后台

- (void)observeAppResignActive:(NSNotification *)noti
{
    [self removeFromSuperview];
}
@end
